package cn.zurish.cloud.kafka.mysql;

import cn.zurish.cloud.kafka.mysql.dao.entity.BarCode;
import cn.zurish.cloud.kafka.mysql.dao.mapper.BarCodeMapper;
import cn.zurish.cloud.kafka.mysql.dao.service.BarCodeService;
import cn.zurish.cloud.kafka.mysql.toolikt.IDGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.zurish.cloud.kafka.mysql.toolikt.FileReaderUtil.readBQFile;


@Slf4j
@Component
@RequiredArgsConstructor
public class ScheduledTasks {


    private final BarCodeMapper mapper;

    private final BarCodeService barCodeService;

    //@Scheduled(cron = "* * * * * ?")
    public void runEveryMinute() {
        // 这里写您希望每分钟执行一次的任务逻辑
        System.out.println("This task runs every seconds.");
    }
    //@Scheduled(fixedRate = 1000)
   // @Scheduled(cron = "* * * * * ?")
    @Scheduled(cron = "10 * * * * *")
    public void runEverySecond() {

        log.info("This task runs every 10 seconds.");
        log.info(" ==========================>");

        String parametersJson = "[{\"techKey\":\"齿距\",\"techValue\":\"1.22-1.25mm\"},{\"techKey\":\"单宽\",\"techValue\":\"2.56-2.60mm\"},{\"techKey\":\"带宽\",\"techValue\":\"14-14mm\"},{\"techKey\":\"单厚\",\"techValue\":\"1.56-1.60mm\"}]";
        String materialsJson = "[{\"materialName\":\"150D/2普缝线(南通)\",\"materialBatch\":\"71502\",\"materialCode\":\"PZ2019121400048\",\"rawName\":\"下线\"},{\"materialName\":\"涤纶DTY150D/48F/重网Z/A13\",\"materialBatch\":\"SF61530/L\",\"materialCode\":\"PZ2019122500026\",\"rawName\":\"上线\"},{\"materialName\":\"3号尼龙新花纹布带\",\"materialBatch\":\"SB530010/LL\",\"materialCode\":\"PZ2024082100030\",\"rawName\":\"布带\"},{\"materialName\":\"N3卧式链条（过渡款）(本厂)\",\"materialBatch\":\"ds00026\",\"materialCode\":\"PZ2024043000136\",\"rawName\":\"链条\"}]";


        try {
            // 示例：读取当前目录下的BQ20250517.txt文件
            String[] contentArray = readBQFile("BQ.txt");
            List<BarCode> list = new ArrayList<>(512);

            // 创建并填充BarCode对象列表


            List<BarCode> barCodeList = new ArrayList<>(contentArray.length);
            for (String line : contentArray) {
                BarCode barCode = new BarCode();
                barCode.setExternalId(IDGenerator.generateId());
                barCode.setProductName(line);
                barCode.setParameters(parametersJson);
                barCode.setMaterials(materialsJson);
                barCodeList.add(barCode);
            }
            // 使用MyBatis-Plus批量插入
            barCodeService.saveBatch(barCodeList);
            log.info("批量插入成功！{}", new Date());

        } catch (IOException e) {
            System.err.println("读取文件时出错: " + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.err.println("文件格式错误: " + e.getMessage());
        }

        // 这里写您希望每秒执行的任务逻辑
        System.out.println("This task runs every second.");
    }
}
